package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.activity.SearchActivity;
import com.jp.knowledge.fragment.CollectionNewsFragment;
import com.jp.knowledge.fragment.CollectionNewsUiFragment;
import com.jp.knowledge.fragment.CollectionRecommendFragment;
import com.jp.knowledge.my.fragment.CollectionAlbumFragment;
import com.jp.knowledge.view.TabView;

import java.util.ArrayList;
import java.util.List;

public class CollectionActivity extends SlidingActivity implements View.OnClickListener {

    private List<Fragment> pagerFragments;

    @Override
    protected int getResourceId() {
        return R.layout.activity_collection;
    }

    @Override
    protected void init() {
        initTitle();
        String[] typeHead = getResources().getStringArray(R.array.collection_head_line_typeName);
        int[] typeId = getResources().getIntArray(R.array.collection_head_line_typeId);
        this.pagerFragments = new ArrayList<>();
        TabView tabView = (TabView) findViewById(R.id.tab_view);
        ViewPager collectionPager = (ViewPager) findViewById(R.id.collection_viewpager);
        collectionPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()){
            @Override
            public int getCount() {
                return pagerFragments.size();
            }
            @Override
            public Fragment getItem(int position) {
                return pagerFragments.get(position);
            }
        });

        for(int i = 0; i < typeId.length; i++){
            CollectionNewsFragment fragment;
            switch (typeId[i]){
                case 15:    // 界面
                    fragment = new CollectionNewsUiFragment();
                    break;
                case 21:    // 行业大观察
                case 17:    // 每日评说
                case 11:    // 全民推荐
                    fragment = new CollectionRecommendFragment();
                    break;
                case 26:    // 专辑
                    fragment = new CollectionAlbumFragment();
                    break;
                case 0:     // 全部
                case 1:     // 头条
                case 3:     // 公司
                case 8:     // 产品
                case 4:     // 人物
                case 22:    // 文库
                case 25:    // 工具
                default:
                    fragment = new CollectionNewsFragment();
                    break;
            }
            pagerFragments.add(fragment.newInstance(typeId[i]));
        }
        collectionPager.getAdapter().notifyDataSetChanged();
        tabView.setupWithViewPager(collectionPager);
        tabView.setTabItems(typeHead);
    }

    private void initTitle(){
        topName.setText("收藏");
        topDesc.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setOnClickListener(this);
        rightIcon.setOnClickListener(this);
        rightIcon.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        if(v == leftIcon)
            finish();
        else if(v == rightIcon)
            startActivity(new Intent(this, SearchActivity.class));
    }
}
